//
//  PlistDemo.swift
//  firstProject
//
//  Created by 张彦辉 on 2017/5/3.
//  Copyright © 2017年 张彦辉. All rights reserved.
//

import UIKit
import SnapKit

class PlistDemo: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        let write1 = UIButton()
        write1.backgroundColor = UIColor.blue
        write1.setTitle("写入NSArray", for: .normal)
        write1.addTarget(self, action: #selector(write1Click), for: .touchUpInside)
        
        let read1 = UIButton()
        read1.backgroundColor = UIColor.darkGray
        read1.setTitle("读取NSArray", for: .normal)
        read1.addTarget(self, action: #selector(read1Click), for: .touchUpInside)
        
        let write2 = UIButton()
        write2.backgroundColor = UIColor.black
        write2.setTitle("写入NSDictionary", for: .normal)
        write2.addTarget(self, action: #selector(write2Click), for: .touchUpInside)
        
        let read2 = UIButton()
        read2.backgroundColor = UIColor.green
        read2.setTitle("读取NSDictionary", for: .normal)
        read2.addTarget(self, action: #selector(read2Click), for: .touchUpInside)
        
        self.view.addSubview(write1)
        self.view.addSubview(write2)
        self.view.addSubview(read1)
        self.view.addSubview(read2)
        
        

        write1.snp.makeConstraints { (maker) in
            maker.top.equalTo(100)
            maker.left.equalTo(10)
        }
        read1.snp.makeConstraints { (maker) in
            maker.top.equalTo(write1)
            maker.left.equalTo(write1.snp.right).offset(7)
        }
        
        write2.snp.makeConstraints { (maker) in
            maker.top.equalTo(180)
            maker.left.equalTo(10)
        }
        read2.snp.makeConstraints { (maker) in
            maker.top.equalTo(write2)
            maker.left.equalTo(write2.snp.right).offset(7)
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

//MARK : - 按钮点击事件
extension PlistDemo{
    func write1Click() -> Void {
        let namesPath = "\(Constant.documentPath)/names.plist"
        let names:NSArray = ["aaa2","bbb","ccc", arc4random() % 100]
        //atomically 是否先写入临时文件
        names.write(toFile: namesPath, atomically: true)
        PublicUtil.alert(obj: self, title: "提示", msg: "NSArray保存成功")
    }
    func read1Click() -> Void {
        let namesPath = "\(Constant.documentPath)/names.plist"
        let entries = NSArray(contentsOfFile: namesPath)!
        PublicUtil.alert(obj: self, title: "提示", msg: "NSArray读取\(entries)")
    }
    
    
    
    func write2Click() -> Void {
        let studentsPath = "\(Constant.documentPath)/students.plist"
        let students: NSDictionary = ["sno": "1102","score": arc4random() % 100]
        //atomically 是否先写入临时文件
        students.write(toFile: studentsPath, atomically: true)
        PublicUtil.alert(obj: self, title: "提示", msg: "NSDictionary保存成功")
    }
    
    func read2Click() -> Void {
        let studentsPath = "\(Constant.documentPath)/students.plist"
        let data = NSDictionary(contentsOfFile: studentsPath)
        PublicUtil.alert(obj: self, title: "提示", msg: "NSDictionary读取\(data!)")
    }
}
